package com.ln.point.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ln.adoms.action.BaseAction;
import com.ln.core.orm.PageData;
import com.ln.core.utils.WebUtils;
import com.ln.point.entity.PointRecord;
import com.ln.point.service.PointRecordService;

@Controller
@RequestMapping("/point/record")
public class PointRecordAction extends BaseAction {
	@Resource
	private PointRecordService recordService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<PointRecord> pageData = new PageData<PointRecord>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = recordService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		return "/point/record/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		PointRecord record = recordService.find(id);
		model.addAttribute(record);
		return "/point/record/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(PointRecord record) {
		if (record.isNew()) {
			recordService.save(record);
		} else {
			recordService.update(record);
		}

		return "redirect:/point/record/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		recordService.delete(id);
		return "redirect:/point/record/list";// 重定向
	}

}
